package com.crl.module.live
{
	import com.crl.utils.FMSUtil;
	
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	
	/**
	 * 视频服务客户端发布接收者抽像类;
	 * @author crl
	 * 
	 */	
	public class AbstractLive extends EventDispatcher
	{
		/**
		 * 当前是否正在运行中; 
		 */		
		protected var isRunning:Boolean=false;
		protected var client:LiveClient;
		protected var stream:NetStream;
		protected var conn:NetConnection;
		protected var connectedCallBack:Function;
		protected var singleLive:SingleLive;
		protected var lostLineCount:int=0;
		
		private var videos:Array;
		
		public function AbstractLive(singleLive:SingleLive)
		{
			if(singleLive==null){
				throw new Error("singleLive is null");
			}
			this.singleLive=singleLive;
			this.client=new LiveClient(this);
		}
		
		public function getStream():NetStream{
			return stream;
		}
		
		public function set videoStatus(bool:Boolean):void{
		}
		
		public function set volume(value:Number):void{
			if(stream){
				var soundTransform:SoundTransform=stream.soundTransform;
				soundTransform.volume=value;
				stream.soundTransform=soundTransform;
			}
		}
		
		
		/**
		 * 关闭当前视频服务; 
		 * 
		 */		
		public function close():void{
			FMSUtil.closeStream(stream);
			
			this.isRunning=false;
			this.singleLive.sharerConnection.getReferences().remove(this);
			this.singleLive.closeVideos();
			
			clearTimeout(timeoutDisconnectInterval);
			if(this.singleLive.sharerConnection.isConnected){
				timeoutDisconnectInterval=setTimeout(doDisconnect,LiveConst.NO_PUBLISHER_DISCONN_TICK);
			}
		}
		
		public function get running():Boolean{
			return this.isRunning;
		}
		
		
		/**
		 * 此处不做流的关闭,流关闭必须自己手动去关闭; 
		 * 
		 */		
		public function dispose():void{
			clearTimeout(timeoutDisconnectInterval);
			clearTimeout(timeoutResetToLiveInterval);
		}
		
		
		protected var timeoutDisconnectInterval:uint=0;
		protected var timeoutResetToLiveInterval:uint=0;
		
		private function doDisconnect():void{
			this.singleLive.sharerConnection.disconnect();
		}
		
		
		public function onMetaData(info:Object):void{
			//sub class todo override it;
			//new NetStreamEvent(NetStreamEvent.ONMETADATA,info);
		}
		
		/**
		 * 视频服务使用者，只关注掉线; 
		 * @param event
		 * 
		 */		
		protected function status(event:NetStatusEvent):void{
			var code:String=event.info.code;
	 		switch(code){
	 			case LiveConst.REJECTED:
	 				break;
				case LiveConst.FAILED:
					
					break;
				case LiveConst.CLOSED:
					break;
	 		}
	 	}
		
	}
}